﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;

public partial class BalanceCenter_EditUser : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            dtUserBirthday.Value = "1970-01-01";

            FillProvince();

            long ID = Shove._Convert.StrToLong(Request.QueryString["ID"],-1);

            if (ID < 0)
            {
                ID = _Customer.ID;
            }

            tbID.Text = ID.ToString();

            FillddlBank();

            BindData(ID);
        }
    }

    #region Web 窗体设计器生成的代码

    override protected void OnInit(EventArgs e)
    {
        isRequestLogin = true;
        SecondLevelPassword = true;
        RequestLoginPage = this.Request.Url.AbsoluteUri;        //获取决对Url

        base.OnInit(e);
    }

    #endregion

    private void FillProvince()
    {
        DataTable dt = new DAL.Tables.tbl_Provinces().Open("", "", "");

        if (dt == null)
        {
            PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读写错误", this.GetType().BaseType.FullName);

            return;
        }

        Shove.ControlExt.FillDropDownList(ddlUserProvince, dt, "Name", "id");

        if (ddlUserProvince.Items.Count > 0)
        {
            ddlUserProvince.SelectedIndex = 0;

            FillCity(int.Parse(ddlUserProvince.SelectedValue));
        }
    }

    private void FillCity(int ProvinceID)
    {
        DataTable dt = new DAL.Tables.tbl_Citys().Open("", "ProvinceID = " + ProvinceID.ToString(), "");

        if (dt == null)
        {
            PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读写错误", this.GetType().BaseType.FullName);

            return;
        }

        Shove.ControlExt.FillDropDownList(ddlUserCity, dt, "Name", "id");

        if (ddlUserCity.Items.Count > 0)
        {
            ddlUserCity.SelectedIndex = 0;
        }
    }

    protected void ddlUserProvince_SelectedIndexChanged(object sender, System.EventArgs e)
    {
        FillCity(int.Parse(ddlUserProvince.SelectedValue));
    }

    private void FillddlBank()
    {
        DataTable dt = new DAL.Tables.tbl_Banks().Open("", "", "[id]");

        if (dt == null)
        {
            PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读写错误", this.GetType().BaseType.FullName);

            return;
        }

        Shove.ControlExt.FillDropDownList(ddlUserBankName, dt, "Name", "ID");
    }

    private void BindData(long id)
    {
        Customer Customer = new Customer(_Site.ID);
        Customer.ID = id;

        string Return = "";

        int Result = Customer.GetCustomerInformationByID(ref Return);
        if (Result != 0)
        {
            PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读写错误", this.GetType().BaseType.FullName);

            return;
        }

        BindData(Customer);
    }

    private void BindData(Customer Customer)
    {
        DataTable dt = new DAL.Tables.tbl_Citys().Open("", "[id]=" + Customer.CityID.ToString(), "");

        if (dt == null)
        {
            PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读写错误", this.GetType().BaseType.FullName);

            return;
        }

        long ProvinceID = -1;

        if (dt.Rows.Count > 0)
        {
            ProvinceID = Shove._Convert.StrToLong(dt.Rows[0]["ProvinceID"].ToString(), -1);
        }

        if (ProvinceID < 0)
        {
            ProvinceID = 1;
            Customer.CityID = 1;
        }

        Shove.ControlExt.SetDownListBoxTextFromValue(ddlUserProvince, ProvinceID.ToString());
        FillCity(int.Parse(ddlUserProvince.SelectedValue));
        Shove.ControlExt.SetDownListBoxTextFromValue(ddlUserCity, Customer.CityID.ToString());

        Shove.ControlExt.SetDownListBoxTextFromValue(ddlUserBankName, Customer.BankTypeID.ToString());

        tbUserName.Text = Customer.CustomerNO;
        tbUserRealityName.Text = Customer.CustomerName;
        tbUserIDCardNumber.Text = Customer.CertNo ;
        //tbUserPassword.Text = tu.Password;
        //tbUserPassword2.Text = tu.Password2;
        tbUserEmail.Text = Customer.Email;

        tbUserTelephone.Text = Customer.Tel;
        tbUserMobile.Text = Customer.Mobile;
        tbUserQQ.Text = Customer.QQ;
        tbUserAddress.Text = Customer.Address;
        if (Customer.Sex == "男")
        {
            rbUserSexM.Checked = true;
        }
        else if (Customer.Sex == "女")
        {
            rbUserSexW.Checked = true;
        }

        dtUserBirthday.Value = Customer.Birthday.ToString("yyyy-MM-dd");

        tbUserBankNameDetail.Text = Customer.BankNameDetail;
        tbUserBankCardNumber.Text = Customer.BankCardNumber;
        tbPayNO.Text = Customer.AlipayNo;

        tbUserRealityName.ReadOnly = true;
        tbUserBankCardNumber.ReadOnly = true;
        tbUserBankNameDetail.ReadOnly = true;

        labUserType.Text = _Customer.CustomerTypeName;

        tbID.Text = Customer.ID.ToString();

        btnSave.Enabled = true;
        btnSave.Visible = true;
    }

    protected void btnSave_Click(object sender, System.EventArgs e)
    {
        if (tbID.Text == "")
        {
            PublicFunction.GoError(ErrorNumber.NoData, "信息错误", this.GetType().BaseType.FullName);

            return;
        }

        Customer Customer = new Customer(_Site.ID);
        Customer.ID = int.Parse(tbID.Text);

        string Return = "";

        int Result = Customer.GetCustomerInformationByID(ref Return);
        if (Result != 0)
        {
            Shove._Web.JavaScript.Alert(this.Page, Return);

            return;
        }

        if (Customer.CustomerNO != tbUserName.Text.Trim())
        {
            Shove._Web.JavaScript.Alert(this.Page, "不能修改用户名。");

            return;
        }

        if (tbUserRealityName.Text.Trim() == "")
        {
            Shove._Web.JavaScript.Alert(this.Page, "请输入真实姓名。");

            return;
        }

        if (tbUserMobile.Text.Trim() == "")
        {
            Shove._Web.JavaScript.Alert(this.Page, "请输入手机号码。");

            return;
        }

        if (tbUserBankNameDetail.Text.Trim() == "")
        {
            Shove._Web.JavaScript.Alert(this.Page, "请输入详细开户银行名称。");

            return;
        }

        if (tbUserBankCardNumber.Text.Trim() == "")
        {
            Shove._Web.JavaScript.Alert(this.Page, "请输入银行卡号。");

            return;
        }

        //if (tbPayNO.Text.Trim() == "")
        //{
        //    Shove._Web.JavaScript.Alert(this.Page, "请输入支付宝号。");

        //    return;
        //}

        Customer.RegisterCustomerNO = Shove._Convert.ToDBC(tbUserRealityName.Text.Trim());
        Customer.Sex = rbUserSexM.Checked ? "男" : (rbUserSexW.Checked ? "女" : "");
        Customer.Birthday = Shove._Convert.StrToDateTime(dtUserBirthday.Value, DateTime.Now.ToShortDateString());

        Customer.Email = Shove._Convert.ToDBC(tbUserEmail.Text.Trim());
        Customer.CityID = int.Parse(ddlUserCity.SelectedValue);

        Customer.CertNo = Shove._Convert.ToDBC(tbUserIDCardNumber.Text.Trim());
        Customer.Tel = Shove._Convert.ToDBC(tbUserTelephone.Text.Trim());
        Customer.Mobile = Shove._Convert.ToDBC(tbUserMobile.Text.Trim());
        Customer.QQ = Shove._Convert.ToDBC(tbUserQQ.Text.Trim());
        Customer.Address = Shove._Convert.ToDBC(tbUserAddress.Text.Trim());
        Customer.BankTypeID = Shove._Convert.StrToShort(ddlUserBankName.SelectedValue, 1);
        Customer.BankNameDetail = tbUserBankNameDetail.Text.Trim();
        Customer.BankCardNumber = Shove._Convert.ToDBC(tbUserBankCardNumber.Text.Trim());
        Customer.AlipayNo = Shove._Convert.ToDBC(tbPayNO.Text.Trim());        

        Return = "";

        if (Customer.EditCustomerByID(ref Return) != 0)
        {
            Shove._Web.JavaScript.Alert(this.Page, Return);

            return;
        }

        if (Return != "")
        {
            Shove._Web.JavaScript.Alert(this.Page, Return);

            return;
        }

        if (Customer.ID == _Customer.ID)
        {
            _Customer.GetCustomerInformationByID(ref Return);
        }


        Shove._Web.JavaScript.Alert(this.Page, "用户资料已经保存成功。");
    }
}